package com.thoughtworks.collection;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.*;

public class Add {
    public int getSumOfEvens(int leftBorder, int rightBorder) {
        int sum = 0;
        if (leftBorder >= rightBorder){
            while (leftBorder>=rightBorder){
                if (rightBorder % 2 == 0){
                    sum+=rightBorder;
                }
                    rightBorder+=1;
            }
        }else if (leftBorder <= rightBorder){
                while (leftBorder<=rightBorder){
                    if (leftBorder % 2 == 0){
                        sum+=leftBorder;
                    }
                    leftBorder+=1;
                }
        }
        return sum;
    }

    public int getSumOfOdds(int leftBorder, int rightBorder) {
        int sum = 0;
        if (leftBorder > rightBorder){
            while (leftBorder>=rightBorder){
                if (rightBorder % 2 != 0){
                    sum+=rightBorder;
                }
                rightBorder+=1;
            }
        }else if (leftBorder < rightBorder){
            while (leftBorder<=rightBorder){
                if (leftBorder % 2 != 0){
                    sum+=leftBorder;
                }
                leftBorder+=1;
            }
        }
        return sum;
    }

    public int getSumTripleAndAddTwo(List<Integer> arrayList) {
        int sum = 0;
        for (int i =0;i<arrayList.size();i++){
            sum+=arrayList.get(i)*3+2;
        }
        return sum;

    }

    public List<Integer> getTripleOfOddAndAddTwo(List<Integer> arrayList) {

        for (int i =0;i<arrayList.size();i++){
            if (arrayList.get(i) % 2 !=0){
                arrayList.set(i,arrayList.get(i)*3+2);
            }

        }
        return arrayList;
    }

    public int getSumOfProcessedOdds(List<Integer> arrayList) {
        int sum = 0;
        for (int i =0;i<arrayList.size();i++){
            if (arrayList.get(i) % 2 !=0){
                sum+=arrayList.get(i)*3+5;
            }
        }
        return sum;
    }
    public List<Integer> getProcessedList(List<Integer> arrayList) {
        List<Integer> list = new ArrayList<>();
        for (int i =0;i<arrayList.size()-1;i++){

            list.add((arrayList.get(i)+arrayList.get(i+1))*3);
        }

        return list;

    }
    public double getMedianOfEven(List<Integer> arrayList) {
        double mid = 0.0;
        List<Integer> list = new ArrayList<>();
        for (int i =0;i<arrayList.size();i++){
            if (arrayList.get(i) % 2 ==0){
                list.add(arrayList.get(i));
            }
        }
        if (list.size() % 2 == 0 ){
            mid = (list.get(list.size()/2-1)+list.get(list.size()/2))/2;
        }else if (list.size() % 2 !=0){
            mid = list.get(list.get(list.size()/2+1));
        }
        return mid;
    }

    public double getAverageOfEven(List<Integer> arrayList) {
        int avg = 0;
        int sum = 0;
        List<Integer> list = new ArrayList<>();
        for (int i =0;i<arrayList.size();i++){
            if (arrayList.get(i) % 2 ==0){
                list.add(arrayList.get(i));
            }
        }
        for (int i = 0; i < list.size(); i++) {
           sum += list.get(i);
        }
        avg = sum/list.size();
        return avg;

    }

    public boolean isIncludedInEvenIndex(List<Integer> arrayList, Integer specialElment) {
        boolean flag = true;
        List<Integer> list = new ArrayList<>();
        for (int i =0;i<arrayList.size();i++){
            if (arrayList.get(i) % 2 ==0){
                list.add(arrayList.get(i));
            }
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == specialElment){
                flag =true;
                break;
            }else {
                flag = false;
                break;
            }
        }
        return flag;

    }

    public List<Integer> getUnrepeatedFromEvenIndex(List<Integer> arrayList) {
        List<Integer> list = new ArrayList<>();
        for (int i =0;i<arrayList.size();i++){
            if (arrayList.get(i) % 2 ==0){
                list.add(arrayList.get(i));
            }
        }
        HashSet set = new HashSet(list);
        list.clear();
        list.addAll(set);
        return list;
    }

    public List<Integer> sortByEvenAndOdd(List<Integer> arrayList) {
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        for (int i =0;i<arrayList.size();i++){
            if (arrayList.get(i) % 2 ==0){
                list1.add(arrayList.get(i));
            }else {
                list2.add(arrayList.get(i));
            }
        }
        Collections.sort(list1);
        Collections.sort(list2,new Comparator<Integer>() {

            @Override
            public int compare(Integer o1, Integer o2) {
                // 返回值为int类型，大于0表示正序，小于0表示逆序
                return o2-o1;
            }
        });
        list1.addAll(list2);
        return list1;
    }


}
